package views.cad;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import components.JImagePanel;

import models.cad.Aluno;
import models.cad.Responsavel;
import models.cad.Usuario;
import supers.GUIInternalFrame;
import supers.GUICadInterface;
import utils.GlobalDeclarations;
import views.SisDskGUI;

import java.awt.CardLayout;

@SuppressWarnings("serial")
public class CadAluGUI extends GUIInternalFrame implements GUICadInterface{
	public JTextField tfNome;
	public JFormattedTextField tfCod;
	public JFormattedTextField tfCodRsp1;
	public JFormattedTextField tfCodRsp2;
	public JTextField tfEmail;
	public JImagePanel pnFoto;
	public JFormattedTextField tfDtNasc;
	public JFormattedTextField tfFone;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JButton btNRsp1;
	public JButton btNRsp2;
	public JLabel lbRsp1;
	public JLabel lbRsp2;
	public JCheckBox cbAtest;
	public JCheckBox cbDesativ;
	public JPanel panelFt;
	
	public CadAluGUI() {
		setTitle("Cadastro de Alunos");
		setSize(new Dimension(365, 345));
		
		tfNome = new JTextField();
		tfNome.setBounds(71, 56, 250, 20);
		getContentPane().add(tfNome);
		tfNome.setColumns(10);
		try {
			tfCod = new JFormattedTextField(new MaskFormatter("###"));
			tfCod.setColumns(3);
			tfCod.setBounds(71, 25, 30, 20);
			getContentPane().add(tfCod);
			
			JLabel lblNewLabel = new JLabel("Cod.:");
			lblNewLabel.setLabelFor(tfCod);
			lblNewLabel.setBounds(27, 28, 34, 14);
			getContentPane().add(lblNewLabel);
			
			JLabel lblNewLabel_1 = new JLabel("Nome:");
			lblNewLabel_1.setLabelFor(tfNome);
			lblNewLabel_1.setBounds(27, 59, 46, 14);
			getContentPane().add(lblNewLabel_1);
			
			tfEmail = new JTextField();
			tfEmail.setColumns(10);
			tfEmail.setBounds(71, 175, 145, 20);
			getContentPane().add(tfEmail);
			
			JLabel label = new JLabel("E-Mail:");
			label.setBounds(27, 178, 46, 14);
			getContentPane().add(label);
			
			tfFone = new JFormattedTextField(new MaskFormatter("(##) ####-####"));
			tfFone.setBounds(71, 206, 96, 20);
			getContentPane().add(tfFone);
			
			JLabel lblNewLabel_2 = new JLabel("Fone.:");
			lblNewLabel_2.setLabelFor(tfFone);
			lblNewLabel_2.setBounds(27, 209, 46, 14);
			getContentPane().add(lblNewLabel_2);
			
			JLabel lblNasc = new JLabel("Nasc.:");
			lblNasc.setBounds(27, 240, 46, 14);
			getContentPane().add(lblNasc);
			
			tfDtNasc = new JFormattedTextField(new MaskFormatter("##/##/####"));
			lblNasc.setLabelFor(tfDtNasc);
			tfDtNasc.setBounds(71, 237, 96, 20);
			getContentPane().add(tfDtNasc);
			
			panelFt = new JPanel();
			panelFt.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panelFt.setBounds(235, 145, 86, 100);
			getContentPane().add(panelFt);
			panelFt.setLayout(new CardLayout(0, 0));
			
			pnFoto = new JImagePanel();
			pnFoto.add(new JLabel("Foto"));
			panelFt.add(pnFoto);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel_1.setBounds(27, 277, 268, 26);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel_1.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel_1.add(btExcluir);
			
			btListar = new JButton("Listar");
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			btListar.setBounds(186, 1, 80, 23);
			panel_1.add(btListar);
			
			JLabel lblCargo = new JLabel("Rsp.:");
			lblCargo.setBounds(27, 91, 34, 14);
			getContentPane().add(lblCargo);
			
			tfCodRsp1 = new JFormattedTextField(new MaskFormatter("###"));
			tfCodRsp1.setColumns(3);
			tfCodRsp1.setBounds(71, 88, 30, 20);
			getContentPane().add(tfCodRsp1);
			
			lbRsp1 = new JLabel(".");
			lbRsp1.setBounds(134, 91, 187, 14);
			getContentPane().add(lbRsp1);
			
			btNRsp1 = new JButton("N");
			btNRsp1.setBounds(103, 87, 23, 23);
			getContentPane().add(btNRsp1);
			
			cbDesativ = new JCheckBox("Aluno Desativado");
			cbDesativ.setBounds(113, 145, 109, 23);
			getContentPane().add(cbDesativ);
			
			cbAtest = new JCheckBox("Atestado");
			cbAtest.setBounds(27, 145, 86, 23);
			getContentPane().add(cbAtest);
			
			JLabel lblResp = new JLabel("coRsp.:");
			lblResp.setBounds(27, 120, 46, 14);
			getContentPane().add(lblResp);
			
			tfCodRsp2 = new JFormattedTextField(new MaskFormatter("###"));
			tfCodRsp2.setColumns(3);
			tfCodRsp2.setBounds(71, 117, 30, 20);
			getContentPane().add(tfCodRsp2);
			
			btNRsp2 = new JButton("N");
			btNRsp2.setBounds(103, 116, 23, 23);
			getContentPane().add(btNRsp2);
			
			lbRsp2 = new JLabel(".");
			lbRsp2.setBounds(134, 120, 187, 14);
			getContentPane().add(lbRsp2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfCod.addFocusListener(fl);
        tfCodRsp1.addFocusListener(fl);
        tfCodRsp2.addFocusListener(fl);
        tfDtNasc.addFocusListener(fl);
        btNRsp1.addActionListener(ac);
        btNRsp2.addActionListener(ac);
        btNRsp1.addFocusListener(fl);
        btNRsp2.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);		
        pnFoto.addMouseListener(ml);			
        panelFt.addMouseListener(ml);
	}

	@Override
	public void limpar(){
		tfCod.setText("");
		tfNome.setText("");
		tfCodRsp1.setText("");
		tfCodRsp2.setText("");
		tfEmail.setText("");
		tfFone.setText("");
		tfDtNasc.setText("");
		lbRsp1.setText("");
		lbRsp2.setText("");
		cbAtest.setSelected(false);
		cbDesativ.setSelected(false);
		pnFoto.clearImage();
		btExcluir.setEnabled(false);
	}

	@Override
	public void setDados(Object obj){
		Aluno alu = (Aluno) obj;
		tfCod.setText(alu.getCodigo());
		tfNome.setText(alu.getNome());
		tfCodRsp1.setText(alu.getResponsavel1().getCodigo());
		tfCodRsp2.setText(alu.getResponsavel2().getCodigo());
		tfEmail.setText(alu.getEmail());
		tfFone.setText(alu.getTelefone());
		tfDtNasc.setText(new SimpleDateFormat("dd/MM/yyyy").format(alu.getDataNasc()));
		cbAtest.setSelected(alu.isAtestado());
		cbDesativ.setSelected(alu.isDesativado());
	}

	@Override
	public Aluno getDados(){
		Aluno alu = new Aluno();
		alu.setCodigo(tfCod.getText());
		alu.setNome(tfNome.getText());
		alu.setResponsavel1(Responsavel.getResponsavel(tfCodRsp1.getText()));
		alu.setResponsavel2(Responsavel.getResponsavel(tfCodRsp2.getText()));
		alu.setEmail(tfEmail.getText());
		alu.setTelefone(tfFone.getText());
		try {
			java.util.Date data = new SimpleDateFormat("dd/MM/yyyy").parse(tfDtNasc.getText());
			alu.setDataNasc(new java.sql.Date(data.getTime()));
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		alu.setAtestado(cbAtest.isSelected());
		alu.setDesativado(cbDesativ.isSelected());
		alu.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		alu.setDtOpe(new Date(System.currentTimeMillis()));
		return alu;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}

}
